library(tidyverse)
library(plm)
library(modelsummary)
options(scipen = 999)

setwd("~/OneDrive - unige.ch/oecd-dac-project-unige/paper-pr-assignment/2023-08-rec-visits")

data <- read.csv("2023-11-02-visits-data-full.csv")
nrow(data) # 16,973

#### 3yr sample ####
# restrict to donor-recipient pairs where a non-zero oda amount was reported anywhere between t and t-2
# where t = year 
# ! Note this removes the 2002-Canada-Ukraine field visit, because Canada doesn't report any oda to Ukraine until 2005
data <- filter(data, commit_dr_any_3yrs == 1) 
nrow(data) # 16,126

names(data)

# outcome: rec_chosen

# predictors, official rules:
# commit_dry_pct_l1
# rec_visited_prevyr

# predictors, other: 
# commit_ry_pct_l1; colonial_match
# wbgi_gee; wbgi_pve; wbgi_rle; p_polity2; wdi_lifexp; wdi_gdpcapcon2010
# ST.INT.ARVL; API_IQ.SCI.OVRL
# unsc OR unsc_ch
# unga_IdealPointDistance_l1

# controls: 
# doc_format, rec_pool_size_3yrs



#### random effects #####
m1 <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
         + rec_visited_prevyr, # rec visited in review at t-1
         data)
summary(m1)

n1b <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
         + rec_visited_prevyr # rec visited in review at t-1
         + commit_ry_pct_l1 # % all dac oda to recipient 
         + colonial_match # Colonial Origin match 
         + wbgi_gee_l1 # Government Effectiveness
         + wbgi_pve_l1 # Political Stability and Absence of Violence/Terrorism 
         + wbgi_rle_l1 # Rule of Law, Estimate
         + p_polity2_l1 # Revised Combined Polity Score
         + wdi_lifexp_l1 # Life expectancy at birth, total (years) 
         + wdi_gdpcapcon2010_l1 # GDP per capita (constant 2010 US dollar) 
         + ST.INT.ARVL_l1 # International tourism, number of arrivals
         + API_IQ.SCI.OVRL_l1 # Statistical capacity
         + unsc_l1 # unsc membership 
         + unga_IdealPointDistance_l1, # unga voting distance
         data)
summary(n1b)


#### control for pool size + doc format #####
m2 <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
         + rec_visited_prevyr # rec visited in review at t-1
         + rec_pool_size_3yrs # control for rec pool size by review 
         + doc_format, # review format fe
         data)
summary(m2)

n2b <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
         + rec_visited_prevyr # rec visited in review at t-1
         + commit_ry_pct_l1 # % all dac oda to recipient 
         + colonial_match # Colonial Origin match 
         + wbgi_gee_l1 # Government Effectiveness
         + wbgi_pve_l1 # Political Stability and Absence of Violence/Terrorism 
         + wbgi_rle_l1 # Rule of Law, Estimate
         + p_polity2_l1 # Revised Combined Polity Score
         + wdi_lifexp_l1 # Life expectancy at birth, total (years) 
         + wdi_gdpcapcon2010_l1 # GDP per capita (constant 2010 US dollar) 
         + ST.INT.ARVL_l1 # International tourism, number of arrivals
         + API_IQ.SCI.OVRL_l1 # Statistical capacity
         + unsc_l1 # unsc membership 
         + unga_IdealPointDistance_l1 # unga voting distance
         + rec_pool_size_3yrs # control for rec pool size by review 
         + doc_format, # review format fe
         data)
summary(n2b)



#### control for pool size + doc format + rec fixed effects #####
m3 <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
          + rec_visited_prevyr # rec visited in review at t-1
          + rec_pool_size_3yrs # control for rec pool size by review 
          + doc_format # review format fe
          + recipient, # recipient fe
          data)
summary(m3)

n3b <- lm(rec_chosen ~ commit_dry_pct_l1 # % donor oda to recipient
         + rec_visited_prevyr # rec visited in review at t-1
         + commit_ry_pct_l1 # % all dac oda to recipient 
         + colonial_match # Colonial Origin match 
         + wbgi_gee_l1 # Government Effectiveness
         + wbgi_pve_l1 # Political Stability and Absence of Violence/Terrorism 
         + wbgi_rle_l1 # Rule of Law, Estimate
         + p_polity2_l1 # Revised Combined Polity Score
         + wdi_lifexp_l1 # Life expectancy at birth, total (years) 
         + wdi_gdpcapcon2010_l1 # GDP per capita (constant 2010 US dollar) 
         + ST.INT.ARVL_l1 # International tourism, number of arrivals
         + API_IQ.SCI.OVRL_l1 # Statistical capacity
         + unsc_l1 # unsc membership
         + unga_IdealPointDistance_l1 # unga voting distance
         + rec_pool_size_3yrs # control for rec pool size by review 
         + doc_format # review format fe
         + recipient, # recipient fe
         data)
summary(n3b)


modelsummary(list(
  "(REs)" = m1,
  "(REs)" = n1b, 
  "(Format FEs + Control for Pool Size)" = m2,
  "(Format FEs + Control for Pool Size)" = n2b,
  "(Format and Recipient FEs + Control for Pool Size)" = m3, 
  "(Format and Recipient FEs + Control for Pool Size)" = n3b), 
  coef_map = c("commit_dry_pct_l1" = "Aid from Donor, Pct of Donor Tot.",
               "rec_visited_prevyr" = "Visited in Previous Year", 
               "commit_ry_pct_l1" = "Aid from DAC Donors, Pct of DAC Donors Tot.",
               "colonial_match" = "Former Colony of Donor", 
               "unsc_l1" = "UNSC Membership",
               "unga_IdealPointDistance_l1" = "UNGA Voting Distance",
               "p_polity2_l1" = "Democracy",
               "wbgi_gee_l1" = "Government Effectiveness", 
               "wbgi_rle_l1" = "Rule of Law", 
               "wbgi_pve_l1" = "Political Stability", 
               "wdi_lifexp_l1" = "Life Expectancy",
               "wdi_gdpcapcon2010_l1" =  "GDP Per Capita",
               "ST.INT.ARVL_l1" = "International Tourism",
               "API_IQ.SCI.OVRL_l1" = "Statistical Capacity"),               
  stars = c("†" = 0.1, "*" = 0.05, "**" = 0.01, "***" = 0.001), # "†" = 0.1, 
  stars_note = TRUE, 
  gof_omit = "DF|Deviance|AIC|BIC|Log.Lik.|F",
  #output = "latex",
  #fmt = prettyNum(digits = 2, format = "g")
  fmt = "%.4f" # number of digits to keep after the period, including trailing zeros
)

